/*
 * @lc app=leetcode.cn id=868 lang=typescript
 *
 * [868] 二进制间距
 */

// @lc code=start
function binaryGap(n: number): number {
    let max = 0;
    let last: number;
    n.toString(2)
        .split("")
        .forEach((i, index) => {
            if (i === "1") {
                if (last !== undefined) {
                    max = Math.max(max, index - last);
                }
                last = index;
            }
        });
    return max;
}
// @lc code=end
